/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.services.impl;

import cz.muni.fi.pa165.creatures.dao.web.PersonDAO;
import cz.muni.fi.pa165.creatures.dto.web.PersonDTO;
import cz.muni.fi.pa165.creatures.dto.web.mapping.FunctionMapping;
import cz.muni.fi.pa165.creatures.dto.web.mapping.PersonMapping;
import cz.muni.fi.pa165.creatures.services.PersonService;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents actual implementation of the
 * {@link cz.muni.fi.pa165.creatures.services.PersonService}.
 *
 * @author xmiklos1
 */
@Service
public class PersonServiceImpl implements PersonService {

    private static final Logger logger =
            Logger.getLogger(PersonServiceImpl.class.getName());

    @Autowired
    PersonDAO personDAO;

    @Autowired
    PersonMapping personMapping;

    @Autowired
    FunctionMapping functionMapping;


    @Override
    @Transactional
    public Long create(PersonDTO person) {
        try {
            return personDAO.create(personMapping.DTOtoEntity(person));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public void remove(PersonDTO person) {
        try {
            personDAO.remove(personMapping.DTOtoEntity(person));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public void update(PersonDTO person) {
        try {
            personDAO.update(personMapping.DTOtoEntity(person));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public PersonDTO getById(Long id) {
        try {
            return personMapping.entityToDTO(personDAO.get(id));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public PersonDTO getByName(String name) {
        try {
            return personMapping.entityToDTO(personDAO.getByName(name));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }
}